package Demo1;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-07-10
 * Time: 10:26
 */
public class SingleLinkedList implements IList{//单链表

    //节点
    static class ListNode{
        public int value;//数值域
        public ListNode next;//下一个节点位置

        public ListNode(int value) {//构造方法
            this.value = value;
        }
    }

    ListNode head = null;//头节点

    public void createNode(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        head = node1;
    }

    //头插法
    @Override
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);//创建新节点

        newNode.next = head;

        head = newNode;
    }

    //尾插法
    @Override
    public void addLast(int data) {
        //先找到尾节点
        ListNode newNode = new ListNode(data);

        //是否为空链表
        if(this.head == null){
            head = newNode;
            return;
        }

        ListNode cur = this.head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = newNode;
    }

    //检查index合法性
    public void checkIndex(int index){
        if(index < 0 || index > size()){
            throw new IndexException("下标Index不合法");
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    @Override
    public void addIndex(int index, int data) {

        try{
            //检查index合法性
            checkIndex(index);
            //是否满足头插条件
            if(this.head == null || index == 0){
                addFirst(data);
                return;
            }

            //判断是否满足尾插条件
            if(index == size() - 1){
                addLast(data);
                return;
            }

            //中间插入
            ListNode newNode = new ListNode(data);
            ListNode cur = this.head;

            //拿到前一个结点
            while(index - 1 > 0){
                cur = cur.next;
                index--;
            }

            //插入
            newNode.next = cur.next;
            cur.next = newNode;
        }catch(IndexException e){
            e.printStackTrace();
        }
    }

    //查找是否包含关键字key是否在单链表当中
    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null){
            if(cur.value == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    @Override
    public void remove(int key) {

        //是否为空链表
        if(head == null){
            return;
        }

        //检查第一个节点
        if(head.value == key){
            head = head.next;
        }

        //找到前一个结点:该方法会跳过第一个节点
        ListNode cur = this.head;
        while(cur.next != null){
            if(cur.next.value == key){

                //删除节点
                ListNode del = cur.next;
                cur.next = del.next;
                break;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    @Override
    public void removeAllKey(int key) {

        //是否为空链表
        if(head == null){
            return;
        }

        //定义前后指针遍历链表:该方法会跳过第一个节点
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while(cur != null){
            if(cur.value == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }

        //检查第一个节点
        if(head.value == key){
            head = head.next;
        }
    }

    //得到单链表的长度
    @Override
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //清空链表
    @Override
    public void clear() {
        ListNode cur = this.head;
        while(cur != null){
            ListNode temp = cur.next;
            cur.next = null;
            cur = temp;
        }
        head = null;
    }

    //打印元素
    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
